package com.sxpi.order.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxpi.order.dto.OrderDto;
import com.sxpi.order.mapper.OrderMapper;
import com.sxpi.order.pojo.Order;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: 董伟豪
 * @Date: 2022/10/25 20:08
 */
@Service
@Slf4j
public class IOrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    /**
     * 下单方法
     * @param orderDto
     * @return
     */
    @Override
    // @LcnTransaction
    @GlobalTransactional
    @Transactional(rollbackFor = Exception.class)
    public Order saveOrder(OrderDto orderDto) {
        Order order = new Order();
        BeanUtils.copyProperties(orderDto,order);
        boolean save = this.save(order);
        return save ? order : null;
    }

    /***
     *
     * 更新订单状态---自动取消
     * @param orderId
     * @return
     */
    @Override
    public boolean updateOrderStatus(Long orderId) {
        Order order = this.getById(orderId);
        if (order != null && order.getStatus().equals(1)) {
            Order newOrder = new Order();
            newOrder.setId(orderId);
            newOrder.setStatus(3);
            this.updateById(newOrder);
            return true;
        }
        return false;
    }

    /**
     * 根据订单编号查询订单信息
     * @param orderNo
     * @return
     */
    @Override
    public Order getOrderInfoByOrderNO(String orderNo) {
        LambdaQueryWrapper<Order> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Order::getOrderNumber, orderNo);
        Order order = this.getOne(lambdaQueryWrapper);
        return order;
    }
}
